package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.adapter.LocateAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.LocateBean;
import com.jason.platform.supervision.beans.LocateResp;
import com.jason.platform.supervision.task.QueryLocateListTask;
import com.jason.platform.supervision.task.QueryRealTimeLocateTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.LoadingDialog;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LocateActivity
 * @Description: 人员定位
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:45:43
 */
public class LocateActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = LocateActivity.class.getSimpleName();

    public static boolean isLoading = false;

    private TitleBar mTitleBar;

    private MyHandler<LocateActivity> mHandler;
    private View mFootView;
    private String mName;
    private String mPhone;

    //搜索
    private EditText mNameEditView, mPhoneEditView;
    private Button mSearchButton;

    private ListView mListView;
    private LocateAdapter mAdapter;
    private List<LocateBean> mLocateBeans = new ArrayList<LocateBean>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_locate);
        super.onCreate(savedInstanceState);
        mHandler = new MyHandler<LocateActivity>(this);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        initSearchView();
        mListView = (ListView) findViewById(R.id.locate_list);
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mAdapter = new LocateAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        } else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    @Override
    protected void initLoadingDialog() {
        super.initLoadingDialog();
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setIsOut(false);
        mLoadingDialog.setMessage("加载中");
    }

    @Override
    protected void initData() {
        super.initData();
        mIsLoading = true;
        mIsStart = true;
        mCurrentIndex = 0;
        getSearchValue();
        QueryLocateListTask task = new QueryLocateListTask(this);
        task.type = Constant.Locate.GET_LOCATE_LIST;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.execute();
    }

    private void initSearchView() {
        mNameEditView = (EditText) findViewById(R.id.search_header_name);
        mPhoneEditView = (EditText) findViewById(R.id.search_header_phone);
        mSearchButton = (Button) findViewById(R.id.search_header_search_btn);
        mSearchButton.setOnClickListener(this);
    }

    private void getSearchValue(){
        mName = mNameEditView.getText().toString();
        mPhone = mPhoneEditView.getText().toString();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_header_search_btn) {
            //TODO 调用接口，进行搜索
            mLoadingDialog.show();
            initData();
        }
    }

    private void loadMore() {
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;
        mIsStart = false;
        getSearchValue();
        QueryLocateListTask task = new QueryLocateListTask(this);
        task.type = Constant.Locate.GET_LOCATE_LIST;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.execute();
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Locate.GET_LOCATE_LIST) {
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                LocateResp resp = (LocateResp) task.resData;
                if (mIsStart){
                    mLocateBeans.clear();
                }
                mLocateBeans.addAll(resp.getValues());
                if (mLocateBeans != null && !mLocateBeans.isEmpty()){
                    if (mAdapter != null){
                        mAdapter.addAll(mLocateBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mHasNextPage = !resp.isLastPage();
                mIsLoading = false;
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
            }else {
                if (mAdapter != null){
                    mAdapter.addAll(new ArrayList<LocateBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(this,task.rspDesc,Toast.LENGTH_SHORT).show();
            }
        } else if (task.type == Constant.Locate.GET_REAL_TIME_LOCATE){
            LocateActivity.isLoading = false;
            if (mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                LocateResp resp = (LocateResp) task.resData;
                //TODO 进入地图
                LocateBean bean = resp.getValues().get(0);
                if (bean.isOpenMap()){
                    Intent intent = new Intent(LocateActivity.this, BaiduMapActivity.class);
                    intent.setAction(BaiduMapActivity.ACTION_LOCATE_LOOK);
                    intent.putExtra(BaiduMapActivity.KEY_LOCATE_LOOK,bean);
                    startActivity(intent);
                }else {
                    Toast.makeText(LocateActivity.this,"暂无坐标信息",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(LocateActivity.this,task.rspDesc,Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void getLocate(LocateBean bean){
        //实时定位
        mLoadingDialog.setMessage("定位中...");
        mLoadingDialog.show();
        QueryRealTimeLocateTask task = new QueryRealTimeLocateTask(LocateActivity.this);
        task.type = Constant.Locate.GET_REAL_TIME_LOCATE;
        task.mParams.put("rymcId", bean.getElectronicsId());
        task.execute();
    }
}
